package com.wjerp.tplus.server.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lishuailei
 */
@Configuration
@EnableSwagger2
public class Swagger2 {

  @Resource
  private ApplicationProperties applicationProperties;

  @Bean
  public Docket createRestApi() {
    List<Parameter> pars = new ArrayList<Parameter>();
    Parameter tmp = new ParameterBuilder()
            .name("Authorization")
            .description("Authorization")
            .modelRef(new ModelRef("string"))
            .parameterType("header").build();

    pars.add(tmp);


    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .host(applicationProperties.getSwaggerHost())
        .groupName("api-v1")
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.wjerp.tplus.server.api.controller"))
        .paths(PathSelectors.any())
        .build()
        .globalOperationParameters(pars)
        .pathMapping(applicationProperties.getSwaggerBasePath());
  }


  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("万景数字 - 基础API")
        .description(apiDescription())
        .contact(new Contact("lishuailei", "www.wjerp.com", "zyfxgary@126.com"))
        .version("1.0")
        .license("The Apache License, Version 2.0")
        .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
        .build();
  }


  /**
   * api描述部分
   *
   * @return
   */
  private String apiDescription() {
    StringBuilder sb = new StringBuilder("任务执行引擎API");
    return sb.toString();
  }
}